import { defineStore } from 'pinia'

interface ReportFile {
  id: string
  name: string
  path: string
  uploadTime: string
}

interface ImageFile {
  id: string
  name: string
  path: string
  uploadTime: string
}

interface WorkflowState {
  currentStep: number
  imageFiles: ImageFile[]
  reportFiles: ReportFile[]
  diagnosis: {
    content: string
    generatedTime: string
  } | null
  stepsValidation: {
    imageUpload: boolean
    reportUpload: boolean
    diagnosis: boolean
  }
}

// 模拟数据
const mockImageFiles: ImageFile[] = [
  {
    id: '1',
    name: 'CT_scan_001.dcm',
    path: '/mock/images/ct_001.dcm',
    uploadTime: '2024-03-20 10:30:00'
  },
  {
    id: '2',
    name: 'MRI_brain_002.dcm',
    path: '/mock/images/mri_002.dcm',
    uploadTime: '2024-03-20 10:31:00'
  }
]

const mockReportFiles: ReportFile[] = [
  {
    id: '1',
    name: '初步诊断报告.pdf',
    path: '/mock/reports/initial_diagnosis.pdf',
    uploadTime: '2024-03-20 11:00:00'
  },
  {
    id: '2',
    name: '病历记录.docx',
    path: '/mock/reports/medical_record.docx',
    uploadTime: '2024-03-20 11:05:00'
  }
]

export const useWorkflowStore = defineStore('workflow', {
  state: (): WorkflowState => ({
    currentStep: 0,
    imageFiles: [...mockImageFiles],
    reportFiles: [...mockReportFiles],
    diagnosis: null,
    stepsValidation: {
      imageUpload: true,
      reportUpload: true,
      diagnosis: false
    }
  }),
  
  actions: {
    setCurrentStep(step: number) {
      this.currentStep = step
    },
    
    addImageFile(file: Omit<ImageFile, 'uploadTime'>) {
      this.imageFiles.push({
        ...file,
        uploadTime: new Date().toISOString()
      })
      this.validateStep('imageUpload')
    },
    
    addReportFile(file: Omit<ReportFile, 'uploadTime'>) {
      this.reportFiles.push({
        ...file,
        uploadTime: new Date().toISOString()
      })
      this.validateStep('reportUpload')
    },
    
    setDiagnosis(content: string) {
      this.diagnosis = {
        content,
        generatedTime: new Date().toISOString()
      }
      this.validateStep('diagnosis')
    },
    
    validateStep(step: keyof WorkflowState['stepsValidation']) {
      switch (step) {
        case 'imageUpload':
          this.stepsValidation.imageUpload = true
          break
        case 'reportUpload':
          this.stepsValidation.reportUpload = true
          break
        case 'diagnosis':
          this.stepsValidation.diagnosis = true
          break
      }
    },
    
    canProceedToStep(step: number): boolean {
      return true
    },
    
    reset() {
      this.currentStep = 0
      this.imageFiles = [...mockImageFiles]
      this.reportFiles = [...mockReportFiles]
      this.diagnosis = null
      this.stepsValidation = {
        imageUpload: true,
        reportUpload: true,
        diagnosis: false
      }
    }
  },
  
  persist: {
    enabled: true,
    strategies: [
      {
        storage: localStorage,
        paths: ['imageFiles', 'reportFiles', 'diagnosis']
      }
    ]
  }
}) 